package event;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.ButtonGroup;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.JRadioButtonMenuItem;

public class Example_6_6 extends JFrame{

	private static final long serialVersionUID = 1L;
	public JPopupMenu pop;
	
	public Example_6_6(){
		super("菜单示例");
		this.setBounds(300, 200, 300, 300);
		this.setDefaultCloseOperation(EXIT_ON_CLOSE);
		
		JMenuBar menuBar = new JMenuBar();
		this.setJMenuBar(menuBar);  //添加菜单栏
		
		JMenu menu_file = new JMenu("文件");
		JMenu menu_help = new JMenu("帮助");
		menuBar.add(menu_file);  //添加菜单
		menuBar.add(menu_help);  //添加菜单
		
		JMenuItem mi1 = new JMenuItem("打开");  //添加菜单项
		JMenuItem mi2 = new JMenuItem("保存");  //添加菜单项
		JMenuItem mi3 = new JMenuItem("关闭");  //添加菜单项
		
		JCheckBoxMenuItem mi4 = new JCheckBoxMenuItem("工具 1");
		JCheckBoxMenuItem mi5 = new JCheckBoxMenuItem("工具 2");
		JRadioButtonMenuItem mi6 = new JRadioButtonMenuItem("格式 1");
		JRadioButtonMenuItem mi7 = new JRadioButtonMenuItem("格式 2");
		
		ButtonGroup bg = new ButtonGroup();
		bg.add(mi6);
		bg.add(mi7);
		
		menu_file.add(mi1);
		menu_file.add(mi2);
		menu_file.add(mi3);
		menu_file.addSeparator();
		menu_file.add(mi4);
		menu_file.add(mi5);
		menu_file.addSeparator();
		menu_file.add(mi6);
		menu_file.add(mi7);
		//--------------以下是JPopupMenu---------------------
		
		pop = new JPopupMenu();
		JMenuItem mi8 = new JMenuItem("复制");  //添加菜单项
		JMenuItem mi9 = new JMenuItem("剪切");  //添加菜单项
		JMenuItem mi10 = new JMenuItem("粘贴");  //添加菜单项
		
		pop.add(mi8);
		pop.add(mi9);
		pop.add(mi10);
		pop.addSeparator();
		
		JMenu menu_format = new JMenu("格式");
		JMenuItem mi11 = new JMenuItem("注释");  //添加菜单项
		JMenuItem mi12 = new JMenuItem("取消注释");  //添加菜单项
		
		menu_format.add(mi11);
		menu_format.add(mi12);
		
		pop.add(menu_format);
		
		this.getContentPane().addMouseListener(new MouseAdapter() {
			
			public void mouseReleased(MouseEvent mouseevent) {
				if(mouseevent.isPopupTrigger()){
					pop.show(getContentPane(), mouseevent.getX(), mouseevent.getY());
				}
			}
			
		});
		
		this.setVisible(true);
	}
	
	public static void main(String[] args) {
		new Example_6_6();
	}

}

